<template>
  <div class="app-page">
    <el-table-form ref="form" :model="query">
      <el-form-item label="角色名称:" prop="roleName">
        <el-input v-model.trim="query.roleName" placeholder="请输入" clearable />
      </el-form-item>
      <template v-slot:btns>
        <el-button v-permission="1" type="primary" icon="el-icon-search" @click="search">查询</el-button>
      </template>
    </el-table-form>
    <div style="margin-bottom: 10px;">
      <el-button v-permission="2" type="primary" icon="el-icon-plus" @click="add">新增</el-button>
    </div>
    <lb-table
      ref="lbTable"
      v-loading="$loadingPlugin.fetchData"
      v-el-height-adaptive-table="{bottomOffset:10}"
      border
      element-loading-text="加载中"
      :column="tableData.column"
      :data="tableData.data"
      highlight-current-row
      pagination
      :total="tableData.total"
      custom-column
      @fetch="fetchData"
    />
    <add-role ref="addRole" :role-type-map="roleTypeMap" />
    <set-permission ref="setPermission" />
    <set-permission-by-app ref="setPermissionByApp" />
  </div>
</template>

<script>
  import { queryMap, roleDelete, roleQueryList } from '@/api'
  import { formatColumnMap, parseTime } from '@/utils'
  import AddRole from './components/AddRole'
  import SetPermission from './components/SetPermission'
  import SetPermissionByApp from './components/SetPermissionByApp'

  export default {
    name: 'roleAuthority',
    components: {
      AddRole,
      SetPermission,
      SetPermissionByApp
    },
    data() {
      return {
        query: {
          roleName: ''
        },
        roleTypeMap: [],
        tableData: {
          total: 0,
          column: [
            {
              type: 'index',
              align: 'center'
            },
            {
              prop: 'roleName',
              label: '角色名称'
            },
            {
              prop: 'roleType',
              label: '角色类型',
              render: (h, { row }) => <span>{ row.roleType && formatColumnMap(this.roleTypeMap, 'value', row.roleType, 'desc') }</span>
            },
            {
              prop: 'remark',
              label: '备注'
            },
            {
              prop: 'createTime',
              label: '创建时间',
              render: (h, { row }) => <span>{ row.createTime ? parseTime(row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') : '' }</span>
            },
            {
              prop: 'createUser',
              label: '创建人'
            },
            {
              label: '操作',
              align: 'center',
              fixed: 'right',
              width: 120,
              render: (h, { row, $index }) => {
                return (
                  <div>
                    <el-link v-permission='3' underline={false} size='mini' onClick={() => this.update(row, $index)}>
                      编辑
                    </el-link>
                    <el-link v-permission='5' underline={false} size='mini' onClick={() => this.permission(row)}>
                      权限
                    </el-link>
                    <el-link v-permission='4' underline={false} size='mini' type='danger' onClick={() => this.del(row, $index)}>
                      作废
                    </el-link>
                  </div>
                )
              }
            }
          ],
          data: []
        }
      }
    },
    created() {
      this.queryOptionsMap()
    },
    methods: {
      _getQuery(obj) {
        const query = {}
        for (const key in obj) {
          if (obj.hasOwnProperty(key)) {
            if (obj[key] !== '') {
              query[key] = obj[key]
            }
          }
        }
        return query
      },
      async fetchData({ currentPage, pageSize }) {
        const query = {
          ...this._getQuery(this.query),
          currentPage,
          pageSize
        }
        const res = await roleQueryList(query)
        this.tableData.data = res.data.rows
        if (currentPage === 1) this.tableData.total = res.data.total
      },
      // 查询枚举
      async queryOptionsMap() {
        const res = await queryMap({ enumName: 'RoleType' })
        this.roleTypeMap = res.data // 角色类型
      },
      async add() {
        await this.$refs.addRole.open()
        this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      async update(row, index) {
        const res = await this.$refs.addRole.open(row)
        this.$set(this.tableData.data, index, res)
      },
      permission(row) {
        if (row.roleType === 3) this.$refs.setPermissionByApp.open(row)
        else this.$refs.setPermission.open(row)
      },
      search() {
        this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      del(row, index) {
        this.$confirm('确认要删除该角色?？', '提示', {
          type: 'warning'
        }).then(() => {
          const loading = this.$message({
            message: '正在删除',
            duration: 0,
            iconClass: 'el-icon-loading'
          })
          roleDelete({ roleId: row.roleId }).then(() => {
            this.tableData.data.splice(index, 1)
            this.$notify({
              title: '提示',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
          }).finally(() => {
            loading.close()
          })
        })
      }
    }
  }
</script>

<style scoped>

</style>
